/* This script contains all the programs used in the ITT analysis and reporting
   for Endline Assessment */

* Program to compute z-score ---------------------------------------------------

/* [z_calculation]
	This small program return a z_score value for a specified variable
	The new column will be named as z_`column'
	Note:
		- z score is calculated against the Control Mean and SD
		- Imputation are made before the z calculation
		- Imputation is for *all* missing rows, but since I remove the officers
			with no survey, the imputation is only on incomplete responses */
	
	capture: program drop z_cal
	program z_cal
	args column
	
			* Get control Mean and SD
			qui: ttest `column', by(treat)
				local control_mean = r(mu_1)
				local control_sd   = r(sd_1)
		
			* Impute missing value with group mean
			gen `column'_imputed = `column'
			replace `column'_imputed = r(mu_1) if missing(`column') & treat == 0
			replace `column'_imputed = r(mu_2) if missing(`column') & treat == 1
		
			* Compute the z-score
			gen z_`column'   = (`column'_imputed - `control_mean') / `control_sd'
			drop `column'_imputed
	end


* Regression -------------------------------------------------------------------

		* ITT ------------------------------------------------------------------
		capture: program drop ITT
		program define ITT
		args outcome with_covar prefix
		
			* Switching covariate on or off
			gl addon 
			if "`with_covar'" ~= "no_covar" {
			gl addon ${`with_covar'}
			}
		
			* Get CM
			qui: sum `outcome' if treat == 0
			local CM = r(mean)
			
			* Get Coef, SE, p, and N
			qui: reg `outcome' treatment i.strata $addon, robust
			qui: test treatment
			mat `prefix'_`outcome' = ///
			`CM', _b[treatment], _se[treatment], r(p), e(N)
		
			* Display
			di "$addon"
			mat li `prefix'_`outcome'
		
		end
		
		* LASSO ----------------------------------------------------------------
		capture: program drop LASSO
		program define LASSO
		args outcome consideration_set prefix
		
			* Get CM
			qui: sum `outcome' if treat == 0
			local CM = r(mean)
			
			* Get Coef, SE, p, and N
			qui: dsregress `outcome' treatment, ///
			controls((i.strata) ${`consideration_set'}) vce(cluster employee_id)
			qui: test treatment 
			mat `prefix'_`outcome' = ///
			`CM', _b[treatment], _se[treatment], r(p), e(N)
			 
			* Store estimates for reference
			* estimates store L_`outcome'
			
			* Display
			di "${`consideration_set'}"
			mat li `prefix'_`outcome'
		
		end
		
		* Versions for interaction term ----------------------------------------
		
		* The folowing programs rely on two list: target_list & covar_list
		capture: program drop ITT_target
		program define ITT_target
		args outcome with_covar prefix
		
			* Get CM
			qui: sum `outcome' if treatment == 0
			local CM = r(mean)
		
			* Switching covariate on or off
			gl addon ${`with_covar'}
			di "$addon"
			* Get Coef, SE, p, and N
			
			qui: reg `outcome' $addon i.strata, robust
			foreach t in $target_list {
				qui: test `t'
				mat `prefix'_`outcome'_`t' = `CM', _b[`t'], _se[`t'], r(p), e(N)
				mat li `prefix'_`outcome'_`t'
			}
		
		end
		
		capture: program drop LASSO_target
		program define LASSO_target
		args outcome consideration_set prefix
		
			* Get CM
			qui: sum `outcome' if treatment == 0
			local CM = r(mean)
			
			* Get Coef, SE, p, and N
			qui: dsregress `outcome' $target_list, ///
			controls((i.strata) ${`consideration_set'}) vce(cluster employee_id)
			
			foreach t in $target_list {
				qui: test `t'
				mat `prefix'_`outcome'_`t' = ///
				`CM', _b[`t'], _se[`t'], r(p), e(N)
				mat li `prefix'_`outcome'_`t'
			}
			 
			* Store estimates for reference
			* estimates store L_`outcome'
			
			* Display
			di "${`consideration_set'}"
		
		end
		
* Q-values ---------------------------------------------------------------------
		
	capture: program drop  p_to_q
	program define p_to_q 
	args
	
	* Create the matrix
		mat temp_p = J(1,5,.)
		foreach member in $family {
			mat temp_p = temp_p \ `member'
		}
	
	* Invoke Anderson code
	preserve
	clear
		
		* Invoke a slot to store p-value
		clear			
		svmat double temp_p, name(pval)
		rename (pval1 pval2 pval3 pval4 pval5) (CM Coef SE pval N)
		drop if _n == 1
		
		* Invoke Anderson code
		qui: do ${FilePath2}/function/0_modified_fdr_sharpened_qvalues.do
		di "Benjamini Krieger Yekutieli (2006) sharpened q-vals"
	
		* Save back q values

			rename bky06 qval
			
				* As single matrix for table purpose
				local counter = 0
				foreach member in $family {
				local counter = `counter' + 1
				mkmat CM Coef SE pval qval N if (_n == `counter'), matrix(q_`member')
				mat li q_`member'
				}
	restore
	end

* Customized Latex for this section --------------------------------------------


* Standard Endline tables 

	* Table Start
	capture: program drop table_start_horizontal
	program table_start_horizontal
	args filename title capsize ColNum 
	
			* Identify number of columns to make
				gl col 
				gl lab
				gl num
				
				forvalues c = 1(1)`ColNum' {
					
						local nm = 4 + `c'
						local cs = 4 + `ColNum' + `c'
						
						gl col ${col}``cs''
						gl lab ${lab} & \multicolumn{1}{c}{\textbf{``nm''}}
						gl num ${num} & \multicolumn{1}{c}{(`c')}

				}
			
			* Set global capsize for box
			gl capsize `capsize'
			
			* Write Expression
			file open table using ${textable}\`filename', write replace
			file write table ///
			"\begin{center} \begin{threeparttable} " _n ///
			"\caption{`title'} \small% " _n ///
			"\begin{tabular}{p{`capsize'pt}${col}} \toprule " _n ///
			"${lab} \\ ${num} \\" _n
			*/
			end
			
	* Write Row
	capture: program drop write_horizontal
	program write_horizontal
	args mat desc version component
	
			* Get the matrix
			mat temp = `mat'
	
			* Get and format estimates
			
				* CM
				local cm :  di %5.3f temp[1,1]
				
					if abs(`cm') > 100 {
				    local cm :  di %3.0fc temp[1,1]
					}
			
				* ITT, SE, N
				local ITT :  di %5.3f temp[1,2]
				local SE  :  di %5.3f temp[1,3]
				local N   :  di %9.0fc temp[1,6]
				
				* P-values
				local p   :  di %5.3f temp[1,4]
				local star = ///
					cond(`p'<=0.01,"***", cond(`p'<=0.05,"**", ///
					cond(`p'<=0.1,"*", cond(`p'>0.1, "", ""))))
				
				if temp[1,4] < 0.001 {
					local p
					local p  "$<$0.001"
				}
				
				
				* Q-values
				local q_`var' : di %5.3f temp[1,5]
				local staq = ///
					cond(`q_`var''<=0.01,"***", cond(`q_`var''<=0.05,"**", ///
					cond(`q_`var''<=0.1,"*", cond(`q_`var''>0.1, "", ""))))
				
				if temp[1,5] < 0.001 {
					local q_`var'
					local q_`var'  "$<$0.001"
				}
			
			* Write a Tex expression
			local cmI \multicolumn{1}{c}{-}
			
			gl inline_desc 			\parbox[m]{${capsize}pt}{\vspace{5pt} `desc' \raggedright \vspace{5pt}} &
			gl inline_CmCoSePvQvN	`cm' & `ITT' & `SE'&`p'\textsuperscript{`star'} & `q_`var''\textsuperscript{`staq'} & `N' \\
			gl inline_CmCoSePvQv	`cm' & `ITT' & `SE'&`p'\textsuperscript{`star'} & `q_`var''\textsuperscript{`staq'}       \\
			gl inline_hCmCoSePvQv	 \multicolumn{1}{c}{-}    & `ITT' & `SE'&`p'\textsuperscript{`star'} & `q_`var''\textsuperscript{`staq'}       \\
			gl inline_RcCoSePvQvN	`cmI' & `ITT' & `SE'&`p'\textsuperscript{`star'} & `q_`var''\textsuperscript{`staq'} & `N' \\
			gl inline_RcCoSePvQv	`cmI' & `ITT' & `SE'&`p'\textsuperscript{`star'} & `q_`var''\textsuperscript{`staq'}       \\
			gl inline_CmCoSePvRq	`cm' & `ITT' & `SE'&`p'\textsuperscript{`star'} 										  \\
			gl inline_CoSePvQv		`ITT' & `SE' & `p'\textsuperscript{`star'} & `q_`var''\textsuperscript{`staq'} 			  \\
			gl inline_CoSePv 		`ITT' & `SE' & `p'\textsuperscript{`star'} & 											  \\
			
			gl write_line			$inline_desc ${inline_`version'}
			
			file write table "$write_line" _n

	end
	
* FOS table
	
	* Table start
	capture: program drop table_start_FOS
	program table_start_FOS
		args filename title
				
				
			file open table using  ${textable}\`filename', write replace
			file write table ///
			"\begin{center} \begin{threeparttable} " _n ///
			"\caption{`title'} \small%" _n ///
			"\begin{tabular}{p{100pt}rrp{1.1cm}p{0.25cm}rrp{1.1cm}p{0.25cm}rrp{1.1cm}p{1.1cm}} " _n ///
			"\toprule" _n ///
			" &  \multicolumn{3}{c}{\textbf{Sit-D}} &  & \multicolumn{3}{c}{\textbf{Direct Threat}} & & \multicolumn{4}{c}{\begin{tabular}[c]{c@{}c@{}} \textbf{Sit-D $ \times $} \\ \textbf{Direct Threat} \end{tabular} }   \\  " _n ///
			"\hhline{ ~===~===~====}" _n ///
			"\\[-1em]" _n ///
			" &  \multicolumn{1}{c}{Coef} &  \multicolumn{1}{c}{SE} & \multicolumn{1}{c}{p-value} & &  \multicolumn{1}{c}{Coef} &  \multicolumn{1}{c}{SE} & \multicolumn{1}{c}{p-value} & &  \multicolumn{1}{c}{Coef} &  \multicolumn{1}{c}{SE} & \multicolumn{1}{c}{p-value} & \multicolumn{1}{c}{q-value} \\ &  \multicolumn{1}{c}{(1)} &  \multicolumn{1}{c}{(2)} &  \multicolumn{1}{c}{(3)} & &  \multicolumn{1}{c}{(4)} &  \multicolumn{1}{c}{(5)} &  \multicolumn{1}{c}{(6)} & &  \multicolumn{1}{c}{(7)} &  \multicolumn{1}{c}{(9)} &  \multicolumn{1}{c}{(9)} & \multicolumn{1}{c}{(10)} \\ " _n 
		
		end
	
	* Inline
	capture: program drop write_i
	program define write_i
	args mat1 mat2 mat3 desc keepcm
	
		foreach s in `mat1' `mat2' `mat3' {
			
			* Get the matrix
			mat temp = `s'

			* Get ITT Estimate, SE, N
			local COEF_`s' 	 :  		di %5.3f  temp[1,2]
			local SE_`s' 	 :  		di %5.3f  temp[1,3]
			local p_`s'		 :  		di %5.3f  temp[1,4]
			local N_`s'   	 :  		di %9.0fc temp[1,6]
			di "A"
			local star_`s' = ///
				cond(`p_`s''<=0.01,"***", ///
				cond(`p_`s''<=0.05,"**", ///
				cond(`p_`s''<=0.1,"*", ///
				cond(`p_`s''>0.1, "", ///
				""))))
			}
			
			if temp[1,4] < 0.001 {
			local p_`s'   
			local p_`s' "$<$0.001"
			}
			
			local q_`mat3' : di %5.3f `mat3'[1,5]
			local staq = ///
					cond(`q_`mat3''<=0.01,"***", cond(`q_`mat3''<=0.05,"**", ///
					cond(`q_`mat3''<=0.1,"*", cond(`q_`mat3''>0.1, "", ""))))

			* Write a Tex expression
			gl write_line `desc' & `COEF_`mat1''	& `SE_`mat1''&`p_`mat1''\textsuperscript{`star_`mat1''} &  & ///
								   `COEF_`mat2''	& `SE_`mat2''&`p_`mat2''\textsuperscript{`star_`mat2''} &  & ///
								   `COEF_`mat3''	& `SE_`mat3''&`p_`mat3''\textsuperscript{`star_`mat3''} &  `q_`mat3''\textsuperscript{`staq'} \\
			file write table " $write_line" _n

	end

* Heterogeneous treatment effect table
	
	* Table start
	capture: program drop table_start_het_Experience
	program table_start_het_Experience
	args filename title
				
		file open table using  ${textable}\`filename', write replace
		file write table ///
		"\begin{center} \begin{threeparttable} " _n ///
		"\caption{`title'} \small%" _n ///
		"\begin{tabular}{p{250pt}rrp{1.1cm}p{0.25cm}rrp{1.1cm}rr} " _n ///
		"\toprule" _n ///
		" & \multicolumn{3}{c}{\textbf{Sit-D}} &  & \multicolumn{3}{c}{\begin{tabular}[c]{c@{}c@{}} \textbf{Sit-D $ \times $} \\ \textbf{Experience} \end{tabular} }  & &   \\   " _n ///
		"\hhline{ ~===~===~~}" _n ///
		"\\[-1em]" _n ///
		" & \multicolumn{1}{c}{Coef} &  \multicolumn{1}{c}{SE} & \multicolumn{1}{c}{p-value} & &  \multicolumn{1}{c}{Coef} &  \multicolumn{1}{c}{SE} & \multicolumn{1}{c}{p-value} & & \multicolumn{1}{c}{N}  \\ &  \multicolumn{1}{c}{(1)} &  \multicolumn{1}{c}{(2)} &  \multicolumn{1}{c}{(3)} & & \multicolumn{1}{c}{(4)} &  \multicolumn{1}{c}{(5)} &  \multicolumn{1}{c}{(6)} & &  \multicolumn{1}{c}{(7)} \\ " _n 
	
	end
	
	* Inline
	capture: program drop write_h
	program define write_h
	args mat_name desc keepcm

			* Get the matrix
			mat T 	= `mat_name'_t
			mat TE 	= `mat_name'_te

			* Get ITT Estimate, SE, N
			local cm 		 :  di %5.3f T[1,1]
			
			if abs(`cm') > 100 {
				    local cm :  di %3.0fc temp[1,1]
			}
			
			if "`keepcm'" == "hidecm" {
				local cm -
			}
			
			foreach s in T TE {
			local COEF_`s' 	 :  		di %5.3f  `s'[1,2]
			local SE_`s' 	 :  		di %5.3f  `s'[1,3]
			local p_`s'		 :  		di %5.3f  `s'[1,4]
			local N_`s'   	 :  		di %9.0fc `s'[1,5]
			
			if `s'[1,4] < 0.001 {
			local p_`s'   :  di %6.4f temp[1,4]
			}
			
			local star_`s' = ///
				cond(`p_`s''<=0.01,"***", ///
				cond(`p_`s''<=0.05,"**", ///
				cond(`p_`s''<=0.1,"*", ///
				cond(`p_`s''>0.1, "", ///
				""))))
			}

			* Write a Tex expression
			gl write_line `desc' \vspace{5pt} & `COEF_T'	& `SE_T'&`p_T'\textsuperscript{`star_T'} &  & ///
											    `COEF_TE'	& `SE_TE'&`p_TE'\textsuperscript{`star_TE'}  &  & `N_T' \\
			file write table "$write_line" _n

	end
	
	
		
	* Mid Rule
	capture: program drop midrule
	program midrule
	file write table "\midrule" _n
	end
	
	*Table bottom
	capture: program drop table_end
	program table_end 
	args note ref
		file write table "\bottomrule \end{tabular}  \begin{tablenotes}" _n ///
		"`note'"
		file write table " \label{`ref'} \end{threeparttable} \end{center}"
		file close table
	end